﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace QuestionInvestig.Common
{
    public class JsonHelper
    {
        /// <summary>
        /// 序列化为json字符串
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns></returns>
        public static string GetJSON(object obj)
        {
            if (obj == null) return "";
            string result;
            DataContractJsonSerializer serializer =
                new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                result = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
            }
            return result;
        }
    }
}
